package com.idemon.ds.controller;

import org.apache.commons.lang.StringUtils;

import com.idemon.commons.util.ValidateKit;
import com.idemon.config.Consts;
import com.idemon.ds.model.Favor;
import com.idemon.ds.model.Result;
import com.idemon.ds.model.Userinfo;
import com.idemon.ds.util.LoginInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;

@Before(LoginInterceptor.class)
public class UserController extends Controller {

	public void index() {
		render("pcenter.html");
	}  

	public void pdetail() {
		Object obj = getSessionAttr(Consts.ATTR_KEY_SESSION_USER);
		if (obj == null) {
			redirect("/account");
		} else {
			setAttr("userinfo", Userinfo.dao.getUserByName(((Userinfo) obj).getUid()));
			render("pcenter.html");
		}
	}

	public void upduserinfo() {
		Userinfo user = getModel(Userinfo.class);
		if (StringUtils.isBlank(user.getPhoneNo()) || StringUtils.isBlank(user.getEmail())
				|| StringUtils.isBlank(user.getWeixin()) || StringUtils.isBlank(user.getQq())) {
			renderJson(new Result(0, "个人信息不能为空"));
			return;
		}
		if (user.getPhoneNo().length() != 11) {
			renderJson(new Result(0, "手机号码必须是11位"));
			return;
		}

		if (!ValidateKit.validate(user.getPhoneNo(), "MOBILE_PHONE")) {
			renderJson(new Result(0, "手机号码不合法"));
			return;
		}

		if (!ValidateKit.validate(user.getEmail(), "EMAIL")) {
			renderJson(new Result(0, "邮箱不合法"));
			return;
		}

		if (!ValidateKit.validate(user.getQq(), "QQ")) {
			renderJson(new Result(0, "QQ不合法"));
			return;
		}

		if (!ValidateKit.validateByRegex(user.getWeixin(), "^[a-zA-Z0-9\\.]{6,20}$")) {
			renderJson(new Result(0, "微信不合法"));
			return;
		}

		if (user.update()) {
			renderJson(new Result(1, "成功"));
			return;
		} else {
			renderJson(new Result(0, "失败"));
			return;
		}
	}

	public void updpass() {
		Object obj = getSessionAttr(Consts.ATTR_KEY_SESSION_USER);
		if (obj == null) {
			renderJson(new Result(0, "请先登陆"));
			return;
		} else {
			String orgpass = getPara("orgpass");
			String newpass = getPara("newpass");
			String repass = getPara("repass");
			if (StringUtils.isBlank(orgpass) || StringUtils.isBlank(newpass) || StringUtils.isBlank(repass)) {
				renderJson(new Result(0, "密码不能为空"));
				return;
			}
			if (orgpass.length() < 6 || newpass.length() < 6 || repass.length() < 6) {
				renderJson(new Result(0, "密码长度不得小于6位"));
				return;
			}
			if (!newpass.equals(repass)) {
				renderJson(new Result(0, "两次输入密码不一致"));
				return;
			}

			if (Userinfo.dao.validate(((Userinfo) obj).getName(), orgpass) == null) {
				renderJson(new Result(0, "原密码不正确"));
				return;
			}

			if (Userinfo.dao.modifyPassword(((Userinfo) obj).getUid(), newpass)) {
				renderJson(new Result(1, "成功"));
				return;
			} else {
				renderJson(new Result(0, "密码修改失败"));
				return;
			}
		}
	}

	public void getmydown() {
		Object obj = getSessionAttr(Consts.ATTR_KEY_SESSION_USER);
		if (obj == null) {
			renderJson(0);
		} else {
			renderJson(Userinfo.dao.getMyDown(((Userinfo) obj).getUid()));
		}
	}

	public void getmyfavor() {
		Object obj = getSessionAttr(Consts.ATTR_KEY_SESSION_USER);
		if (obj == null) {
			renderJson(0);
		} else {
			renderJson(Userinfo.dao.getMyFavor(((Userinfo) obj).getUid()));
		}
	}

	public void addfavor() {
		Object obj = getSessionAttr(Consts.ATTR_KEY_SESSION_USER);
		if (obj != null) {
			int uid = ((Userinfo) obj).getUid();
			int sid = getParaToInt("sid");
			if (Favor.dao.isFavored(uid, sid) || Favor.dao.saveFavor(uid, sid)) {
				renderJson(1);
				return;
			}
		}
		renderJson(0);
	}

}
